using MediatR;
using SmartApplication.Api.Applient.Command.Feed;
using SmartApplication.ApiCode;
using SmartApplication.Domain.Entity;
using SmartApplication.Infrastructure.Instrument;

namespace SmartApplication.Api.Applient.CommandHandler.Feed
{
    public class UpdateHealthHandler : IRequestHandler<UpdateHealthCommand, ApiResult<int>>
    {
        public UpdateHealthHandler(IBaseRepository<HealthModel> healthRepository)
        {
            HealthRepository = healthRepository;
        }

        public IBaseRepository<HealthModel> HealthRepository { get; }

        /// <summary>
        /// 修改保健信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(UpdateHealthCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<int>();

            var health = await HealthRepository.GetValueAsync(request.Id);
            health.SourceId = request.SourceId;
            health.FeedDays = request.FeedDays;
            health.HealthType = request.HealthType;
            health.HealthWay = request.HealthWay;

            var update = await HealthRepository.UpdateAsync(health);
            if (update > 0)
            {
                res.Code = EnumCode.Success;
                res.Message = "修改保健信息成功";
                res.Data = update;
                return res;
            }
            else
            {
                res.Code = EnumCode.Fail;
                res.Message = "修改保健信息失败";
                res.Data = update;
                return res;
            }
        }
    }
}